package br.com.goldentrade.util.table;

import br.com.goldentrade.util.MoedaFormat;
import java.awt.Component;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import jdaap.gui.components.data.DateUtil;


public class ColumnRenderer extends DefaultTableCellRenderer
{
	
	private SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
	/*@requisicao [2389]*/
	private NumberFormat formatter = MoedaFormat.getInstance().getFormatoMoeda();
	
	public ColumnRenderer(){}
	
	public ColumnRenderer(SimpleDateFormat sdf)
	{
		this.sdf = sdf;
	}
	
	public ColumnRenderer(String dateFormat)
	{
		this(new SimpleDateFormat(dateFormat));
	}
	
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		
		if(Boolean.class.equals(table.getColumnClass(column))){
			JCheckBox check = new JCheckBox();
			if(value instanceof Boolean && ((Boolean)value).booleanValue()){
				check.setSelected(true);
			}
			
			setHorizontalAlignment(SwingConstants.CENTER);
			check.setHorizontalAlignment(SwingConstants.CENTER);
			
			if(isSelected){
				check.setBackground(table.getSelectionBackground());
				check.setForeground(table.getSelectionForeground());
			}else{
				check.setBackground(table.getBackground());
				check.setForeground(table.getForeground());
			}
			
			return check;
		}
		
		return comp;
	}

	protected void setValue(Object value) 
	{
		if (value instanceof ImageIcon) 
		{			
			ImageIcon icon = (ImageIcon) value;
			setIcon(icon);
			if (icon.getDescription()!=null && !icon.getDescription().equals("")) //$NON-NLS-1$
				setToolTipText(icon.getDescription());
			
		}else if (value instanceof Date){
			setHorizontalAlignment(CENTER);
			if (sdf != null)
				setText(sdf.format((Date)value));
			
			else
				super.setValue(value);
			
		}else if (value instanceof java.sql.Date){
			
			Date data = DateUtil.convertSQLDateToDate((java.sql.Date)value);
			if (sdf != null)
				setText(sdf.format((Date)data));
			else
				super.setValue(data);
			
		}else if (value instanceof java.sql.Timestamp){
			
			Date data = DateUtil.convertTimestampToDate((java.sql.Timestamp)value);
			if (sdf != null)
				setText(sdf.format((Date)data));
			else
				super.setValue(data);
			
		}else if (value instanceof java.sql.Time){
			
			java.sql.Time time = ((java.sql.Time)value);
			if (sdf != null)
				setText(sdf.format(time));
			else
				super.setValue(time);
			
		}else if (value instanceof Double){
			
			Double valor = (Double) value;
			this.setHorizontalAlignment(SwingConstants.RIGHT);
			setText( formatter.format(valor));   
			
		}else if (value ==null){
			super.setValue("");
		}
		else {
			this.setHorizontalAlignment(SwingConstants.LEFT);
			super.setValue(value);
		}
	}
}
